using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.Practices.Web.UI.WebControls;
using Ornament.MemberShip;
using Ornament.MemberShip.Module.Presenters;
using Ornament.MemberShip.Module.Views;

public partial class Admin_MemberAdmin_Controls_EditUserGroupViewCtl : UserControl, IUserGroupEditView
{
    public string[] SelectedRoleIds
    {
        get { return RoleAssignView.SelectedRoleIds; }
    }

    public bool IsEditUser
    {
        get { return false; }
    }

    #region IUserGroupEditView Members

    public Guid CurrentId
    {
        get
        {
            if (ListView1.SelectedValue != null)
                return (Guid) ListView1.SelectedValue;
            return Guid.Empty;
        }
    }

    public IRoleAssignView RoleAssignView
    {
        get { return RoleAssign1; }
    }

    public UserGroupEditorPresenter Presenter { get; set; }

    public void BindUserGroupList(IList<UserGroup> list, int totalRecord)
    {
        UserGroupListDataSource.DataSource = list;
        UserGroupListDataSource.TotalRowCount = totalRecord;
    }


    public int PageSize
    {
        get
        {
            var pager = (DataPager) ListView1.FindControl("DataPager1");
            return pager == null ? 20 : pager.PageSize;
        }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter = new UserGroupEditorPresenter(this);

        if (!IsPostBack)
        {
            Presenter.InitilizeView();
        }
        Presenter.ViewLoad();
    }


    protected void showForAdd(object sender, EventArgs e)
    {
        ListView1.InsertItemPosition = InsertItemPosition.LastItem;
        RoleAssign1.DataBind();
    }

    protected void ObjectContainerDataSource1_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
    {
        Presenter.BindUserGroups(e.Arguments.StartRowIndex);
    }


    protected void SaveUserGroup(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.Save((UserGroup) e.Instance);
        
    }

    protected void CreateUserGroupForAllOperator(object sender, ObjectContainerDataSourceCreatingObjectArgs e)
    {
        switch (e.CurrentOperation)
        {
            case ObjectContainerOperator.Insert:
                e.NewObjectInstance = new UserGroup();
                break;
            default:
                {
                    Guid id = Guid.Empty;
                    foreach (Guid ids in e.Keys.Values)
                    {
                        id = ids;
                    }
                    e.NewObjectInstance = Presenter.GetUserGroup(id);
                    break;
                }
        }
    }


    protected void CancelEditModel(object sender, EventArgs e)
    {
        ListView1.InsertItemPosition = InsertItemPosition.None;
    }

    protected void ReBindRole_OnItemEditing(object sender, ListViewEditEventArgs e)
    {
       
    }

    protected void DeleteAll(object sender, EventArgs e)
    {
        var result = new List<Guid>();
        foreach (ListViewDataItem item in ListView1.Items)
        {
            var checkBox = (HtmlInputCheckBox) item.FindControl("checkBoxSelected");
            if (checkBox.Checked)
            {
                result.Add(new Guid(checkBox.Value));
            }
        }

        Presenter.Delete(result.ToArray());
    }

    protected void SelectUserGroupForRoleAssign(object sender, EventArgs e)
    {
        RoleAssignView.CurrentId = ListView1.DataKeys[ListView1.SelectedIndex].Value.ToString();
        RoleAssignView.Presenter.InitilizeView();

    }

    protected void SaveRoleAssign1(object sender, EventArgs e)
    {
        Presenter.SaveAssignResult();
    }
}